--行会主殿功能
--#include "data\config\guild\Guild.lua" once

GUILD_GUARD_UNDO	=	1				--未开始
GUILD_GUARD_DOING	=	2				--进行中
GUILD_GUARD_DONE	=	3				--已结束

--[[
注册一些事件
]]
function ResisterGuildGuardProc()
	--print("ResisterGuildGuardProc...")
	for guardIdx, guardConfig in ipairs( GuildConfig.guildGuard ) do
		SceneExitDispatcher.register( guardConfig.sceneId, OnExitGuildGuard )						--注册离开守护行会副本后触发的事件
		FubenTimeoutDispatcher.register( guardConfig.fubenId, OnGuildGuardFubenTimeout )			--注册守护行会副本时间到后触发的事件
	end
end

--[[
退出守护行会副本
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitGuildGuard( sysarg, sceneId, fubenId )
	--print("OnExitGuildGuard, sceneId="..sceneId..", fubenId="..fubenId)
	CloseActivityRightPanel(sysarg)				--关闭面板
end

--[[
副本时间到
]]
function OnGuildGuardFubenTimeout( fubenPtr )
	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )			--时间到的副本handle
	local guildId 		= Fuben.getGuildId( fubenHandle )
	--[[因为Fuben.setFubenTime( fubenHandle, 1 )会导致副本延迟1秒执行撤销，所以Fuben.setFubenTime( fubenHandle, 1 )后，再立即进行创建新副本
	nowFubenHandle可能不等于fubenHandle
	]]
	local guildGuardSts, nowFubenHandle = GetGuildGuardStsAndHandle( guildId )	--当前正在进行的副本handle
	--print("OnGuildGuardFubenTimeout, guildId="..guildId..", fubenHandle="..fubenHandle..", nowFubenHandle="..nowFubenHandle)
	if nowFubenHandle ~= fubenHandle then
		return
	end
	SetGuildGuardStsAndHandle( guildId, GUILD_GUARD_DONE, 0 )
	Guild.nofityGuildGuardSts( guildId, GUILD_GUARD_DONE )			--通知行会成员活动结束	
	------------------------------有代码-----------------------------
end

--[[
在守卫行会中
]]
function IsInGuildGuardFuben( fubenId )
	for i, cfg in ipairs( GuildConfig.guildGuard ) do
		if cfg.fubenId == fubenId then
			return true
		end
	end
	return false
end


--[[
守护行会今日开启次数
]]
function GetGuildGuardDailyOpenTimes( guildId )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId]		= {}
	end
	return dVar.guildGuard[guildId][3] or 0
end

function SetGuildGuardDailyOpenTimes( guildId, newTimes )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId]		= {}
	end

	dVar.guildGuard[guildId][3] = newTimes
end

--[[
守护行会怪物波数
]]
function GetGuildGuardMonsterIdx( guildId )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId] = {}
	end
	return dVar.guildGuard[guildId][4] or 0
end

function SetGuildGuardMonsterIdx( guildId, newIdx )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId] = {}
	end

	dVar.guildGuard[guildId][4] = newIdx
end

--[[
守护行会怪物击杀个数
]]
function GetGuildGuardMonsterAliveNum( guildId )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId] = {}
	end
	return dVar.guildGuard[guildId][5] or 0
end

function SetGuildGuardMonsterAliveNum( guildId, newNum )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId] = {}
	end

	dVar.guildGuard[guildId][5] = newNum
end

function GetGuildGuardMonsterAllNum( monsters )
	local num = 0
	for freshIdx, freshCfg in ipairs( monsters ) do
		for monIdx, oneMon in ipairs( freshCfg ) do
			num = num + oneMon.num
		end
	end
	return num
end

function GetGuildGuardStsAndHandle( guildId )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId]		= {}
		dVar.guildGuard[guildId][1] 	= GUILD_GUARD_UNDO
		dVar.guildGuard[guildId][2] 	= 0
	end

	local guildGuardSts 	= dVar.guildGuard[guildId][1] or GUILD_GUARD_UNDO
	local guildGuardHandle 	= dVar.guildGuard[guildId][2] or 0
	return guildGuardSts, guildGuardHandle
end

function SetGuildGuardStsAndHandle( guildId, newSts, newHandle )
	local dVar = System.getDyanmicVar()
	if not dVar.guildGuard then
		dVar.guildGuard = {}
	end

	if not dVar.guildGuard[guildId] then
		dVar.guildGuard[guildId]		= {}
		dVar.guildGuard[guildId][1] = GUILD_GUARD_UNDO
		dVar.guildGuard[guildId][2] = 0
	end

	dVar.guildGuard[guildId][1] = newSts
	dVar.guildGuard[guildId][2] = newHandle
end

--[[开启守卫行会
开启对应的副本
]]
function OpenGuildGuard( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return
	end

	local guildPos = Actor.getGuildPos(sysarg)
	if guildPos ~= smGuildLeader and guildPos ~=smGuildAssistLeader and guildPos ~= smGuildTangzhu then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0034,ttFlyTip)
		return
	end

	local bossCfg = GuildConfig.guildGuard[guildLevel]
	if not bossCfg then
		return
	end

	local fubenId = Actor.getFubenId( sysarg )
	if IsInGuildGuardFuben( fubenId ) then 		--已经在副本中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.GuildGuard006,ttFlyTip)
		return
	end

	if not CheckTimeValid(bossCfg.openTime) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildGuard003, ttFlyTip)
		return
	end

	--print("OpenGuildGuard, guildId="..guildId..", guildLevel="..guildLevel)
	local guildGuardSts,  fubenHandle = GetGuildGuardStsAndHandle( guildId )
	--print("OpenGuildGuard, guildGuardSts="..guildGuardSts)
	if guildGuardSts == GUILD_GUARD_DOING then			--正在进行中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0043,ttFlyTip)
		return
	end

	local openTimes = GetGuildGuardDailyOpenTimes( guildId )
	--print("OpenGuildGuard, openTimes="..openTimes)
	if openTimes >= #bossCfg.openConsume then				--守卫行会次数
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildGuard005, ttFlyTip)
		return
	end

	local nextOpenTime = openTimes + 1
	local needGuildCoin = bossCfg.openConsume[nextOpenTime]
	if not needGuildCoin then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildGuard005, ttFlyTip)
		return
	end

	local hasGuildCoin = Actor.getGuildCoin(sysarg)
	--print(" OpenGuildGuard, nextOpenTime="..nextOpenTime..", needGuildCoin="..needGuildCoin..", hasGuildCoin="..hasGuildCoin)
	if needGuildCoin > hasGuildCoin then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0083, ttFlyTip)
		return
	end

	if not Guild.changeGuildCoin(guildId, -needGuildCoin) then		--减少行会资金
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0083, ttFlyTip)
		return
	end

	SetGuildGuardDailyOpenTimes( guildId, nextOpenTime )
	
	--[[
	--print("OpenGuildGuard, guildId="..guildId..", guildLevel="..guildLevel)
	local guildGuardSts,  fubenHandle = GetGuildGuardStsAndHandle( guildId )
	--print("OpenGuildGuard, guildGuardSts="..guildGuardSts)
	if guildGuardSts == GUILD_GUARD_DOING then			--正在进行中
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0043,ttFlyTip)
		return
	elseif guildGuardSts == GUILD_GUARD_DONE then			--今天已经结束了
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0042,ttFlyTip)
		return
	end
	]]

	--print("OpenGuildGuard, old fubenHandle="..fubenHandle)
	if fubenHandle > 0 then
		--Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0037,ttFlyTip)
		--return
		Fuben.setFubenTime( fubenHandle, 1 )				--手工离开，关闭正在进行的副本
	end
	
	fubenHandle = Fuben.createFuBen( bossCfg.fubenId )			--创建一个新的副本
	--print("OpenGuildGuard, guildId="..guildId..", fubenHandle="..fubenHandle)
	if fubenHandle <= 0 then			--创建副本失败
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0091, ttFlyTip)
		return
	end

	local fubenPtr = Fuben.getFubenPtr( fubenHandle )
	if not fubenPtr then
		return
	end

	SetGuildGuardStsAndHandle( guildId, GUILD_GUARD_DOING, fubenHandle )
	Fuben.setGuildId( fubenHandle, guildId )				--设置副本相关的行会id
	--System.broadcastTipmsg( Lang.ScriptTips.GuildGuard001, ttScreenCenter+ttChatWindow )

	SetGuildGuardMonsterIdx( guildId, 0 )								--怪物波数清0
	local allMonNum = GetGuildGuardMonsterAllNum( bossCfg.monsters )
	SetGuildGuardMonsterAliveNum( guildId, allMonNum )					--需要击杀的怪物数量
	local fresnNum = #bossCfg.monsters
	FreshCommonMonsters( { bossCfg.guard}, fubenHandle )
	Fuben.unregScriptCallback(fubenPtr, "LoopGuildGardFreshMonster")
	Fuben.regScriptTimer2S(fubenPtr, 0, bossCfg.freshBegin*1000, bossCfg.freshInterval*1000, fresnNum, "LoopGuildGardFreshMonster", guildId)

	System.broadcastGuildMsg(guildId, Lang.ScriptTips.GuildGuard001)
	Guild.nofityGuildGuardSts( guildId, GUILD_GUARD_DOING, nextOpenTime )			--通知帮派成员活动开启
end

--[[
进入对应的副本
]]
function EnterGuildGuard( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0033, ttFlyTip)
		return
	end

	local bossCfg = GuildConfig.guildGuard[guildLevel]
	if not bossCfg then
		return
	end

	if not Actor.canTelport(sysarg, bossCfg.sceneId) then  --是否允许场景传送
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0020,ttFlyTip)
		return
	end

	local guildGuardSts, fubenHandle = GetGuildGuardStsAndHandle( guildId )
	if guildGuardSts == GUILD_GUARD_UNDO then				--尚未开始
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038,ttFlyTip)
		return
	elseif guildGuardSts == GUILD_GUARD_DONE then			--已经结束
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0042,ttFlyTip)
		return
	end

	--print("EnterGuildGuard, guildId="..guildId..", fubenHandle="..fubenHandle)
	if fubenHandle <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038,ttFlyTip)
		return
	end

	if not Fuben.isFubenExist( fubenHandle ) then			--副本已经不存在（被撤销）了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildGuard004,ttFlyTip)
		return
	end

	local enterPos = bossCfg.enterPos
	if Actor.enterFuBen(sysarg, fubenHandle, bossCfg.sceneId, enterPos[1], enterPos[2]) then
		OpenGuildGuardPanel( sysarg )
	end
end


--[[
打开大乱斗面板
]]
function OpenGuildGuardPanel( sysarg )
	local  fubenId = Actor.getFubenId( sysarg )
	if not IsInGuildGuardFuben( fubenId ) then
		return
	end
	local fubenPtr = Actor.getFubenPrt(sysarg)

	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		return
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		return
	end

	local bossCfg = GuildConfig.guildGuard[guildLevel]
	if not bossCfg then
		return
	end

	local activityType 	= 1 			--活动
	local activityId 	= enRightPanelActivity_GuildGuard
	local restTime 		= Fuben.getFbTime( fubenPtr )
	if restTime < 0 then
		restTime = 0
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	panelInfo.title 	= Lang.ScriptTips.GuildGuardName
	panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime	= restTime

	local monsterIdxAll = #bossCfg.monsters
	local monsterIdxNow = GetGuildGuardMonsterIdx( guildId ) or 0
	panelInfo.contentTitle 	= ""  			--不用显示
	panelInfo.contents 		= {}
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont24, monsterIdxAll )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont25, monsterIdxNow )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end

function ChangeGuildGuardPanel( sysarg , monsterIdxAll, monsterIdxNow )
	local  fubenId = Actor.getFubenId( sysarg )
	if not IsInGuildGuardFuben( fubenId ) then
		return
	end

	local panelInfo 		= {}
	panelInfo.sts 			= 1
	panelInfo.contents 		= {}
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont24, monsterIdxAll )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont25, monsterIdxNow )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜

	ChangeActivityRightPanel( sysarg, panelInfo )
end

--[[
全副本同步阵营积分
]]
function BoardGuildGardChangePanel( fubenPtr, guildId )
	local guildLevel = System.getGuildbuildingLevel(guildId, gbtMainHall)
	if guildLevel < 1 then
		return
	end

	local bossCfg = GuildConfig.guildGuard[guildLevel]
	if not bossCfg then
		return
	end

	local monsterIdxAll = #bossCfg.monsters
	local monsterIdxNow = GetGuildGuardMonsterIdx( guildId ) or 0
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
		ChangeGuildGuardPanel( player, monsterIdxAll, monsterIdxNow )
	end
end

--[[
定时器轮询处理
]]
function LoopGuildGardFreshMonster( fubenPtr, guildId )
	guildId = tonumber(guildId)
	local monIdx = GetGuildGuardMonsterIdx( guildId )
	local monIdx = monIdx + 1
	--print("LoopGuildGardFreshMonster, guildId="..guildId..", monIdx="..monIdx)
	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		return
	end
	local fubenHandle = Fuben.getFubenHandle( fubenPtr )
	local monsters = GuildConfig.guildGuard[guildLevel].monsters[monIdx]
	FreshCommonMonsters( monsters, fubenHandle, true )

	SetGuildGuardMonsterIdx( guildId, monIdx )
	BoardGuildGardChangePanel( fubenPtr, guildId )
end

--[[
守卫行会的守卫被击杀
]]
function OnGuildGuardGuardDie( guardPtr, sysarg, guardId, lastHitKiller )
	local fubenPtr 		= Actor.getFubenPrt( guardPtr )
	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
	local guildId 		= Fuben.getGuildId( fubenHandle )	
	--print("OnGuildGuardGuardDie, guardId="..guardId..", guildId="..guildId)
	local guildLevel = System.getGuildbuildingLevel(guildId, gbtMainHall)
	if guildLevel < 1 then
		return
	end

	local guardCfg = GuildConfig.guildGuard[guildLevel]
	if not guardCfg then
		return
	end

	OnGuildGuardOver( fubenPtr, 0, guardCfg )			--活动失败
end

--[[
守卫行会的怪物被击杀
]]
function OnGuildGuardMonsterDie( monsterPtr, sysarg, monsterId, lastHitKiller )
	--print("OnGuildGuardMonsterDie, monsterId="..monsterId)
	local fubenPtr 		= Actor.getFubenPrt( monsterPtr )
	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
	local guildId 		= Fuben.getGuildId( fubenHandle )
	--print("OnGuildGuardMonsterDie, monsterId="..monsterId..", guildId="..guildId)
	if guildId <= 0 then
		return
	end

	local guildLevel = System.getGuildbuildingLevel(guildId, gbtMainHall)
	if guildLevel < 1 then
		return
	end

	local guardCfg = GuildConfig.guildGuard[guildLevel]
	if not guardCfg then
		return
	end


	local aliveNum = GetGuildGuardMonsterAliveNum( guildId )
	aliveNum = aliveNum - 1
	--print("OnGuildGuardMonsterDie, aliveNum="..aliveNum)
	SetGuildGuardMonsterAliveNum( guildId, aliveNum )
	if aliveNum <= 0 then
		OnGuildGuardOver( fubenPtr, 1, guardCfg )			--胜利
	end
end


function GetGuildGuardCfgByGuardId( guardId )
	for guardIdx, guardCfg in ipairs( GuildConfig.guildGuard ) do
		if guardCfg.guard.monsterId == guardId then
			return guardCfg
		end
	end
	return nil
end

--[[
刷出守卫行会副本类的怪物
]]
function FreshGuildGuard( bossCfg, fubenHandle )
	FreshCommonMonsters( bossCfg.monsters, fubenHandle )
end

function BroadGuildGuardSts( fubenPtr )
	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		SendGuildGuardSts( player )
	end
end

--下发守卫行会活动状态
function SendGuildGuardSts( sysarg )
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		return
	end
	local guildGuardSts, fubenHandle = GetGuildGuardStsAndHandle( guildId )
	local openTimes = GetGuildGuardDailyOpenTimes( guildId ) or 0
	--print("SendGuildGuardSts, guildGuardSts="..guildBossSts)
	Guild.sendGuildGuardSts( sysarg, guildGuardSts, openTimes )
end

function OnGuildGuardOver( fubenPtr, sts, guardCfg )
	if sts == 1 then	--成功发奖励
		local title 	=  Lang.ScriptTips.GuildGuardMailTitle
		local content 	=  Lang.ScriptTips.GuildGuardMailContent
		local playerList = LuaHelp.getFbActorList( fubenPtr )
		for k, player in ipairs( playerList or {} ) do
			local actorId = Actor.getActorId( player )
			--print("OnGuildBossAllDie， actorId="..actorId)
			Awards.GiveToMailById( actorId, guardCfg.awards, title, content, GameLog.clGuildGuardSuccessMail, Lang.ScriptTips.GuildLog008 )
		end
	end

	BroadGuildGuardOver( fubenPtr, sts, guardCfg )
	local fubenHandle = Fuben.getFubenHandle( fubenPtr )
	local guildId = Fuben.getGuildId( fubenHandle )	
	--[[
	SetGuildGuardStsAndHandle( guildId, GUILD_GUARD_DONE, fubenHandle )
	Guild.nofityGuildGuardSts( guildId, GUILD_GUARD_DONE )
	]]
	Fuben.setFubenTime( fubenHandle, 1 )				--关闭正在进行的副本, 会触发 OnGuildGuardFubenTimeout

	if sts == 0 then
		System.broadcastGuildMsg(guildId, Lang.ScriptTips.GuildGuard007)
	else
		System.broadcastGuildMsg(guildId, Lang.ScriptTips.GuildGuard008)
	end
end

function BroadGuildGuardOver( fubenPtr, sts, guardCfg )
	--print("BroadGuildGuardOver, sts="..sts)
	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		GuildGuardOver( player, sts, guardCfg )
	end
end

function GuildGuardOver( sysarg, sts, guardCfg )
	local panelInfo = {}
	panelInfo.subActivityId = 0
	panelInfo.sts = sts 			--1-胜利，0-失败
	if panelInfo.sts == 1 then
		panelInfo.awards = guardCfg.awards
	end
	PassActivityRightPanel( sysarg, 1, enRightPanelActivity_GuildGuard, panelInfo )
end


ResisterGuildGuardProc()				--注册事件

